require 'digest/md5'
require 'mechanize'
require 'xmlsimple'
module WWW
  class Ali
    class Util
      class Tool
        ##获得最后解析结果
        def Tool::get_result params
          agent = WWW::Mechanize.new{|a| a.log = Logger.new("mech.log")}
          url = get_url(params)
          page = agent.get(url)
          ret_val = page.body.to_s
          RAILS_DEFAULT_LOGGER.info 'params1:'+url
          RAILS_DEFAULT_LOGGER.info 'result1:'+ret_val
          begin
            if ret_val!=nil||ret_val==''
              data = XmlSimple.xml_in(ret_val)
              return data
            end
          rescue Exception
          end
        end
        #获得最后提交的url
        def Tool::get_url params
          url = WWW::Ali.get_sip_url_pre
          keys = params.keys.sort
          index = 0
          keys.each do|key|
            if index==0
              url = url+"?"
            else
              url = url+"&"
            end
            url = url+key+"="+params[key].to_s
            index=index+1
          end
          return url
        end
        #获得签名信息
        def Tool::get_sip_sign hash
          sign_str = WWW::Ali.get_cert_code
          keys = hash.keys.sort
          keys.each do|key|
            sign_str=sign_str+key+hash[key].to_s
          end
          return Digest::MD5.hexdigest(sign_str).upcase
        end
      end
    end
  end
end
